// C_funcs.c - BC's C function library

//////////////////////////////////////////////////////////////////////////////
// File includes
//

// System includes
#include <ctype.h>
#include <string.h>
#include <stdio.h>

//+===========================================================================
// Read a single line from the input file
// If line is too long the return value will be >max
// This routine WILL return blank lines!
//

int     freadl(FILE* f, char* l, int max)
{

char    ch;
int     i;

  *l = '\0';

  for (i = 0; i+1<max; i++)
  {
    ch = fgetc(f);
    if (feof(f))  break ;

    // handle all forms of line termination
    if ((ch=='\r')||(ch=='\n'))
    {
      char    ch2 = fgetc(f);
      if (feof(f))  break ;

      if ( (ch2==ch) || ((ch2!='\r')&&(ch2!='\n')) )
        fseek(f, -1, SEEK_CUR);

      break;
    }

    *(l+i) = ch;
    *(l+i+1) = '\0';
  }

  return(i);

}

//+===========================================================================
// Strips prefixing and suffixing spaces and tabs from a string
// returns the length of the edited string
//

int     strim(char* s)
{

char*   st;
char*   nd;
int     len;

  for (st=s; isspace(*st); st++);
  if ( (len = strlen(st)) )
  {
    for (nd=&st[len-1]; isspace(*nd); nd--);
    len = nd-st+1;
    memmove(s, st, len);
  }

  s[len] = '\0';
  return(len);

}

// C_funcs.c [EOF]
